package com.zls.mapper;

import com.zls.pojo.Branch;
import com.zls.pojo.Item;
import com.zls.pojo.Subitem;
import com.zls.pojo.Taskplan;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface ItemMapper {
    @Results(id = "ItemMap", value = {
            @Result(column = "iid", property = "iid"),
            @Result(column = "iname", property = "iname"),
            @Result(column = "starttime", property = "starttime"),
            @Result(column = "endtime", property = "endtime"),
            @Result(column = "updatetime", property = "updatetime"),
            @Result(column = "suid", property = "suid"),
            @Result(column = "suid", property = "subitem", javaType = Subitem.class, one =
                    //FetchType.LAZY 懒加载   EAGER:立即加载
                    //select 代表的关联查询  调用指定的mapper方法
            @One(select = "com.com.zls.mapper.SubitemMapper.findBySuid", fetchType = FetchType.EAGER))
    })
    @Select("Select * from item where status =1")
    List<Item> findAll();

    @ResultMap("ItemMap")
    @Select("<script>" +
            "select * from item where status =1" +
            "<if test='queryString!=null'> and iname like concat( '%',#{queryString},'%')" +
            "limit #{currentPage},#{pageSize}</if>" +
            "<if test='queryString==null'>limit #{currentPage},#{pageSize}</if> "+
            "</script>")
    List<Item> findItem(@Param("currentPage") Integer currentPage, @Param("pageSize") Integer pageSize, @Param("queryString") String queryString);

    //查询总记录数
    @Select("<script>" +
            "select count(*) from item where status =1" +
            "<if test='queryString!=null'> and iname like concat( '%',#{queryString},'%') </if>" +
            "</script>")
    Long count(@Param("queryString") String queryString);

    @Insert("insert into Item (iid,iname,starttime,endtime,updatetime,suid)" +
            "values (#{iid},#{iname},#{starttime},#{endtime},#{updatetime},#{suid})")
    int addItem(Item item);

    //根据iid查询
//    @ResultMap("ItemMap")
    @Select("select * from Item where status =1 and iid=#{iid}")
    Item findByIid(String iid);

    //修改
    @Update("update Item set iname=#{iname},starttime=#{starttime},suid=#{suid},endtime=#{endtime},updatetime=#{updatetime} where iid=#{iid} and status =1")
    int updateItem(Item item);

    //逻辑删除
    @Update("update Item set status=0 where iid=#{iid}")
    int deleteItem(String iid);

    @Select("select * from Item where status =1 and suid=#{suid}")
    List<Item> findBySuid(String suid);
}
